
package exception.setter;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.e885.log.E885Logger;

import common.CommonHttpServlet;

/**
 * url:<br>
 * 1.http://127.0.0.1:8080/JSP/exception/setter?name=grady.kao, <br>
 * 2.http://127.0.0.1:8080/JSP/exception/setter?name=grace, 模擬發生 SetterErrorException<br>
 * 3.http://127.0.0.1:8080/JSP/exception/setter, 模擬發生 SetterNullException<br>
 * <br>
 * 目前在 error.jsp 顯示亂碼, 但不知要如何解決
 * 
 * @author Eric.Kao(gau168.gau@msa.hinet.net)
 * @version 1.0
 * @company E885 Group
 * @copyright Copyright (c) 2001-2008
 */

public class SetterExceptionServlet extends CommonHttpServlet
{
	private static final long serialVersionUID = 4238182768572731701L;

	protected void service( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException
	{
		String name = request.getParameter( "name" );

		if( name == null )
		{
			E885Logger.servletLog.debug( "name is null" );
			
			request.setAttribute( "javax.servlet.jsp.jspException", new SetterNullException( "姓名是 Null") );
			request.getRequestDispatcher( "/exception/setter/error.jsp" ).forward( request, response );
		}
		else if( !name.equals( "grady.kao" ) )
		{
			E885Logger.servletLog.debug( "name is error" );
			
			request.setAttribute( "javax.servlet.jsp.jspException", new SetterErrorException( "姓名錯誤") );
			request.getRequestDispatcher( "/exception/setter/error.jsp" ).forward( request, response );
		}
		else
		{
			E885Logger.servletLog.debug( "name is correct" );
			
			request.setAttribute( "Name", name );
			request.getRequestDispatcher( "/exception/info.jsp" ).forward( request, response );
		}
	}
}
